﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using UserDefineForm.dao.supplierform;
using System.Configuration;
using System.Collections.Specialized;
using UserDefineForm.dao.orm;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace UserDefineForm
{
    public class SystemFactory
    {
        private static SqlConnection sqlConnection;
        private static ControlDAO controlDAO;
        private static SupplierFormDAO formDAO;
        private static ListItemDAO listItemDAO;

        private static SupplierFormORM currentForm;
        private static SupplierFormService formService;

        private static SqlConnection getSQLConnection()
        {
            if (sqlConnection == null)
            {
                String serverName = ConfigurationManager.AppSettings.Get("dbservername");
                String dbName = ConfigurationManager.AppSettings.Get("dbname");

                String connectionString =  string.Format("Data Source={0};Initial Catalog={1};Integrated Security=SSPI", serverName, dbName);
                
                sqlConnection = new SqlConnection(connectionString);
            }
            return sqlConnection;
        }

        public static ControlDAO getControlDAO()
        {
            if (controlDAO == null)
            {
                controlDAO = new ControlDAO(getSQLConnection());
            }
            return controlDAO;
        }

        public static SupplierFormDAO getFormDAO()
        {
            if (formDAO == null)
            {
                formDAO = new SupplierFormDAO(getSQLConnection());
            }
            return formDAO;
        }

        public static ListItemDAO getListItemDAO()
        {
            if (listItemDAO == null)
            {
                listItemDAO = new ListItemDAO(getSQLConnection());
            }
            return listItemDAO;
        }

        public static SupplierFormORM getCurrentForm()
        {
            if (currentForm == null)
            {
                currentForm = new SupplierFormORM();
            }
            return currentForm;
        }

        public static void setCurrentForm(SupplierFormORM orm)
        {
            currentForm = orm;
        }

    }
}
